// pages/userOrder/userOrder.js
const orderService = require('../../service/orderService.js')
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '0',
    pageParam: {
      pageNo: 1,
      pageSize: 15
    },
    reqDelay: false,
    isEmpty: true,
    totalPage: '',
    xcxOrderList: [],
    showToast: false,
    toastMsg: ''
  },

  //错误提示
  showToastFuc(msg) {
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.type) {
      this.setData({
        type: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (option) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      xcxOrderList: [],
      reqDelay: false,
      isEmpty: true,
      pageParam: {
        pageNo: 1,
        pageSize: 15
      }
    })
    this.getMyOrder()
  },

  changeType(event){
    console.log(event.currentTarget.dataset.type)
    if (event.currentTarget.dataset.type != this.data.type){
      this.setData({
        xcxOrderList: [],
        type: event.currentTarget.dataset.type
      })
      this.getMyOrder()
    }
  },
  getMyOrder(){
    this.setData({
      reqDelay: true
    });
    var params = this.data.pageParam
    params.orderStatus = this.data.type
    orderService.listXcxOrder(params).then(data => {
      if (data.code == 200) {
        this.setData({
          totalPage: data.response.page.totalPage
        })
        var orderList = data.response.xcxOrderList
        var totalList = {}
        //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
        if (!this.data.isEmpty) {
          totalList = this.data.xcxOrderList.concat(orderList);
        }else {
          totalList = orderList;
          this.data.isEmpty = false;
        }
        this.setData({
          xcxOrderList: totalList,
          reqDelay: false
        });
        wx.hideNavigationBarLoading();
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch((e) => {
        console.log(e)
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.reqDelay) {
      if (this.data.pageParam.pageNo >= this.data.totalPage) {
        console.log("没有了")
      } else {
        console.log(2222222222)
        wx.showNavigationBarLoading()
        this.setData({
          pageParam: {
            pageNo: this.data.pageParam.pageNo + 1,
            pageSize: 10
          },
        })
        this.getMyOrder()
      }
    }
  }
})